﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class test : MonoBehaviour {

	public AudioSource ass = null;
	public VideoPlayer vp = null;

	// Use this for initialization
	int count = 0;
	void Start () {





		if (ResLoader.HaveDownload ("http://116.196.107.84/video/03.mp4") && ResLoader.HaveDownload ("http://116.196.107.84/audio/02.mp3")) {
			vp.source = VideoSource.Url;
			vp.url = ResLoader.Url2LocalPath ("http://116.196.107.84/video/03.mp4");
			vp.prepareCompleted += pred;
			vp.Prepare ();

		} else {
			Loading.GetInstance ().StartLoading ();
			ResLoader.Load ("http://116.196.107.84/video/03.mp4", null, (www) => {
				count++;

				vp.url = ResLoader.Url2LocalPath ("http://116.196.107.84/video/03.mp4");
				vp.prepareCompleted += pred;
				vp.Prepare ();

				Loading.GetInstance ().EndLoading ();

					
			}, true, true);
			ResLoader.Load ("http://116.196.107.84/audio/02.mp3", null, (www) => {
				count++;

//				AudioClip ac = WWWAudioExtensions.GetAudioClip (www);
//				ass.clip = ac;

				if (count >= 2)
					Loading.GetInstance ().EndLoading ();
				
			}, true, true);

		}
	}

	bool isready= false;
	void pred(VideoPlayer vpv)
	{
		Debug.Log	("set ready true");
		isready = true;
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public void OnBegin()
	{
		if (!isready) {
			Debug.Log ("is ready false");
			return;
		}
		

//		ass.Play ();

//		vp.Play ();
//		vp.Pause();
		vp.Play ();


	}

	public void Close()
	{
//		ass.Stop ();
		vp.Pause ();



		gameObject.SetActive (false);
	}
}
